require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/iconfont.css");
require("./movement_cyclinging.less");

// 引入模块
let axios = require("../../utils/axios.js");
let doc = require("../../utils/dom");
let getHMS = require("../../utils/getHMS.js");

document.addEventListener("DOMContentLoaded", function () {
	let meter = 0; //初始化总公里数
	let duration = 0; //时长
	let calorie = 0; //卡路里
	let id = 0;
	// 获取随机数代替配速
	function time() {
		id = setInterval(() => {
			doc.get("#speed").textContent = random(7, 13);
			meter += random(7, 13);
			doc.get("#meter").textContent = (meter / 1000).toFixed(2);
			doc.get("#duration").textContent = getHMS.setHMS(duration++);
			doc.get("#calorie").textContent = ((meter * 1.05) / 100).toFixed(2);
		}, 1000);
	}
	function random(min, max) {
		return Math.floor(Math.random() * (max - min + 1) + min);
	}

	// 开启定时器
	time();

	// 点击暂停
	doc.get(".icon-24gf-pauseCircle").addEventListener("click", function () {
		doc.get(".btn").style.display = "none";
		doc.get(".btnbox").style.display = "flex";
		clearInterval(id);
	});

	// 点击继续
	doc.get(".icon-bofang").addEventListener("click", () => {
		doc.get(".btn").style.display = "block";
		doc.get(".btnbox").style.display = "none";
		time();
	});

	// 点击结束
	doc.get(".icon-guanji-").addEventListener("click", () => {
		axios
			.post("/api/exercise/save", {
				type: 0,
				duration,
				calorie,
				meter,
			})
			.then((res) => {
				console.log(res);
				if (res.data.errno === 0) {
					alert("少年下次打卡等着你");
					this.location.href = "movement_cycling.html";
				} else {
					alert("数据提交失败");
				}
			});
	});
});
